/*
 * Copyright (C) 2004 by Friederich Kupzog Elektronik & Software
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
    
    Author: Friederich Kupzog  
    fkmk@kupzog.de
    www.kupzog.de/fkmk
 */
package com.sophialex.health.record.common.showKtable;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

import com.sophialex.health.common.platform.SuperContext;

import de.kupzog.ktable.KTableModel;
import de.kupzog.ktable.KTableSortComparator;
import de.kupzog.ktable.KTableSortedModel;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.renderers.FixedCellRenderer;

/**
 * @author Friederich Kupzog
 */
public class HeaderCellRenderer extends FixedCellRenderer {
	private Font font = SuperContext.f10;
	private Color beginColor = new Color(Display.getDefault(), new RGB(253,
			253, 253));
	private Color endColor = new Color(Display.getDefault(), new RGB(227, 227,
			227));

	public void setBeginColor(Color beginColor) {
		this.beginColor = beginColor;
	}

	public void setEndColor(Color endColor) {
		this.endColor = endColor;
	}

	public HeaderCellRenderer(int style) {
		super(style);
	}

	public void drawCell(GC gc, Rectangle rect, int col, int row,
			Object content, boolean focus, boolean fixed, boolean clicked,
			KTableModel model) {
		applyFont(gc);
		// super.drawCell(gc, rect, col, row, content, focus, fixed, clicked,
		// model);
		Color bgColor = getBackground();
		Color bottomBorderColor = COLOR_LINE_DARKGRAY;
		Color rightBorderColor = COLOR_LINE_DARKGRAY;
		Color fgColor = getForeground();
		if (focus && (m_Style & INDICATION_FOCUS) != 0) {
			bgColor = COLOR_FIXEDHIGHLIGHT;
			bottomBorderColor = COLOR_TEXT;
			rightBorderColor = COLOR_TEXT;
		}
		if (focus && (m_Style & INDICATION_FOCUS_ROW) != 0) {
			bgColor = COLOR_BGROWFOCUS;
			bottomBorderColor = COLOR_BGROWFOCUS;
			rightBorderColor = COLOR_BGROWFOCUS;
			fgColor = COLOR_FGROWFOCUS;
		}
		rect = drawDefaultSolidCellLine(gc, rect, bottomBorderColor,
				rightBorderColor);
		gc.setBackground(bgColor);
		gc.setForeground(fgColor);
		gc.fillRectangle(rect);

		Font oldFont = gc.getFont();
		gc.setFont(font);

		if (beginColor != null && endColor != null) {
			Color oldForeground = gc.getForeground();
			Color oldBackground = gc.getBackground();
			gc.setBackground(beginColor);
			gc.setForeground(endColor);
			gc.fillGradientRectangle(rect.x, rect.y, rect.width, rect.height,
					true);
			gc.setBackground(oldBackground);
			gc.setForeground(oldForeground);// 还原前景色
		}

		if (col == 0
				&& row == 0
				&& (content.toString() == null || content.toString().equals(""))) {
			// Color oldForeground = gc.getForeground();
			// int oldLineWidth = gc.getLineWidth();
			// // 绘制水平线
			// gc.drawLine(rect.x + 1, rect.y + 1, rect.x + rect.width - 2,
			// rect.y + rect.height - 2);
			// gc.setFont(oldFont);// //还原字体
			// gc.setForeground(oldForeground);// 还原前景色
			// gc.setLineWidth(oldLineWidth);// 还原线宽
		} else {
			Image indicator = null;

			if ((m_Style & INDICATION_SORT) != 0
					&& model instanceof KTableSortedModel
					&& ((KTableSortedModel) model).getSortColumn() == col
					&& model.getFixedHeaderRowCount() > row) {

				int sort = ((KTableSortedModel) model).getSortState();
				if (sort == KTableSortComparator.SORT_UP)
					indicator = IMAGE_ARROWDOWN;
				else if (sort == KTableSortComparator.SORT_DOWN)
					indicator = IMAGE_ARROWUP;
			}

			if (indicator != null) {
				SWTX.drawTextImage(gc, content.toString(), getAlignment(),
						null, getAlignment(), rect.x + 0, rect.y + 2,
						rect.width - 8, rect.height - 4);
				gc.setFont(oldFont);

				//排序的图片
				gc.fillRectangle(rect.x + rect.width - 8, rect.y, indicator
						.getBounds().width + 8, rect.height);
				gc.drawImage(indicator, rect.x + rect.width - 8, rect.y
						+ rect.height / 2 - indicator.getBounds().height / 2);
			} else {
				SWTX.drawTextImage(gc, content.toString(), getAlignment(),
						null, getAlignment(), rect.x + 0, rect.y + 2,
						rect.width - 0, rect.height - 4);
				gc.setFont(oldFont);
			}
		}
	}
}
